package com.pan.admin.module.channel.entity.vo;

import com.pan.admin.common.constant.module.StatisticEnum;
import com.pan.admin.common.utils.StatisticUtils;
import com.pan.admin.module.channel.entity.LoanChannelRateStatistic;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 渠道统计返回前端值
 *
 * @author Pan
 */
@Data
public class LoanChannelRateStatisticVO {

    /**
     * 链接名称
     */
    private String name;
    /**
     * 总数
     */
    private List<Integer> registerCount;
    private List<Integer> formCount;
    private List<String> registerRate;
    private List<String> formRate;
    /**
     * 创建时间
     */
    private List<String> createTime;

    public static List<LoanChannelRateStatisticVO> fill(List<LoanChannelRateStatistic> list, StatisticEnum statisticEnum) {
        List<LoanChannelRateStatisticVO> listVO = new ArrayList<>();
        Map<String, List<LoanChannelRateStatistic>> groupMap = list.stream().collect(Collectors.groupingBy(t -> t.getName()));
        //  获取填充时间
        List<String> fillTime = StatisticUtils.getFillTime(statisticEnum);

        Iterator<Map.Entry<String, List<LoanChannelRateStatistic>>> iterator = groupMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<LoanChannelRateStatistic>> next = iterator.next();
            String name = next.getKey();
            List<LoanChannelRateStatistic> value = next.getValue();

            LoanChannelRateStatisticVO statisticVO = new LoanChannelRateStatisticVO();
            statisticVO.setName(name);
            statisticVO.setRegisterCount(new ArrayList<>());
            statisticVO.setRegisterRate(new ArrayList<>());
            statisticVO.setFormCount(new ArrayList<>());
            statisticVO.setFormRate(new ArrayList<>());
            statisticVO.setCreateTime(new ArrayList<>());

            for (int i = 0; i < fillTime.size(); i++) {
                String time = fillTime.get(i);
                //  查找该时间段内是否存在数据
                Optional<LoanChannelRateStatistic> first = value.stream().filter(t -> t.getCreateTime().equals(time)).findFirst();
                if (first.isPresent()) {
                    LoanChannelRateStatistic find = first.get();
                    //  存在则按顺序添加值
                    statisticVO.getRegisterCount().add(find.getRegisterCount());
                    statisticVO.getRegisterRate().add(new BigDecimal(find.getRegisterRate()).setScale(2, BigDecimal.ROUND_DOWN).toString());
                    statisticVO.getFormCount().add(find.getFormCount());
                    statisticVO.getFormRate().add(new BigDecimal(find.getFormRate()).setScale(2, BigDecimal.ROUND_DOWN).toString());
                } else {
                    statisticVO.getRegisterCount().add(0);
                    statisticVO.getRegisterRate().add("0");
                    statisticVO.getFormCount().add(0);
                    statisticVO.getFormRate().add("0");
                }
                statisticVO.getCreateTime().add(time);
            }
            listVO.add(statisticVO);
        }
        return listVO;
    }

}
